<%--
  Created by IntelliJ IDEA.
  User: A
  Date: 2024/4/26
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css">
    <script src="../../static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/kindeditor/kindeditor.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .ok{
            color: green;
        }
        .error{
            color: red;
        }
    </style>
</head>
<body>
    <%--添加form表单--%>

    <form class="layui-form" action="">
        <br>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车品牌</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="brandId" name="brandId" lay-filter="brandId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车型号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="model" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车车牌号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="licensePlate" id="licensePlate" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input"  onblur="checkLicensePlate()">
            </div>
            <span id="licensePlateMsg"></span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车照片</label>
            <div class="layui-input-inline layui-input-wrap">
                <div style="width: 132px;">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" width="150px" height="150px" class="layui-upload-img" id="demo1">
                        <div id="demoText"></div>
                        <input type="hidden" name="image" id="imageId">
                    </div>
                </div>
                <button type="button" class="layui-btn" id="uploadId">
                    <i class="layui-icon layui-icon-upload"></i> 照片上传
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生产年份</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="year" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车颜色</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="color" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车介绍</label>
            <div class="layui-input-inline layui-input-wrap">
                <textarea id="detailsId" name="details" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">租金</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="rentalPrice" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">汽车分类</label>
            <div class="layui-input-inline layui-input-wrap">
                <select id="categoryId" name="categoryId" lay-filter="categoryId">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="submitAdd">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script>
        function checkLicensePlate () {
            var licensePlate = $('#licensePlate').val();
            var regex = /^(?:京|津|沪|渝|蒙|辽|吉|黑|苏|浙|皖|闽|赣|鲁|豫|鄂|湘|粤|桂|琼|川|贵|云|藏|陕|甘|青|宁|新|港|澳|台)[A-Z][DF][0-9A-HJ-NP-Z]{5}$|^(?:京|津|沪|渝|蒙|辽|吉|黑|苏|浙|皖|闽|赣|鲁|豫|鄂|湘|粤|桂|琼|川|贵|云|藏|陕|甘|青|宁|新|港|澳|台)[A-Z][0-9A-HJ-NP-Z]{5}$/;
            if (regex.test(licensePlate)) {
                $('#licensePlateMsg').html('该车牌号格式正确');
                $('#licensePlateMsg').removeClass();
                $('#licensePlateMsg').addClass('ok');
            } else {
                $('#licensePlateMsg').html('该车牌号格式错误');
                $('#licensePlateMsg').removeClass();
                $('#licensePlateMsg').addClass('error');
            }
        };
        layui.use(['form','upload'], function(){
            var form = layui.form;
            var upload = layui.upload;
            var kindEditorParams = {
                afterBlur: function () {
                    this.sync();
                }
            };
            var editor = KindEditor.create("#detailsId", kindEditorParams);
            $.post(
                '/brand?method=selectAll',
                function (result) {
                    if (result.code == 0) {
                        $(result.data).each(function () {
                            $('#brandId').append('<option value="'+this.brandId+'">'+this.brandName+'</option>');
                        })
                        form.render('select');
                    }
                },
                'json'
            );
            $.post(
                '/category?method=selectAll',
                function (result) {
                    if (result.code == 0) {
                        $(result.data).each(function () {
                            $('#categoryId').append('<option value="'+this.categoryId+'">'+this.categoryName+'</option>');
                        })
                        form.render('select');
                    }
                },
                'json'
            );
            var uploadInst = upload.render({
                elem: '#uploadId',
                url: '/upload', // 实际使用时改成您自己的上传接口即可。
                before: function(obj){
                    // 预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); // 图片链接（base64）
                    });
                },
                done: function(result){
                    // 若上传失败
                    if(result.code == 0){
                        $('#imageId').val(result.data);
                        $('#demoText').html(''); //置空上传失败的状态
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                error: function(){
                    // 演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                },
            });
            form.on('submit(submitAdd)',function (data) {
                var field = data.field;
                console.log(data);
                $.post(
                    '/car?method=add',
                    field,
                    function (result) {
                        console.log(result);
                        if (result.code == 0) {
                            mylayer.okMsg(result.msg);
                            // layer.close(index);
                            // table.reload('tableId');
                            setInterval(function() {
                                //关闭弹出框
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            }, 2000)
                        } else {
                            mylayer.errorMsg(result.msg);
                        }
                    },
                    'json'
                );
                return false;
            });
        });

    </script>
</body>
</html>
